using System;
using System.Collections.Generic;
using System.Xml;

namespace Thinxoft.HttpLib
{
	
    
	public class Settings : MarshalByRefObject
	{
		MimeTypes mimetypes;
        HttpApplicationCollection apps;        
        XmlDocument config;
        public const string GLOBAL_SETTINGS_FILE = "data/xedusconfig.xml";
        List<char> blockedChars;
       

        public Settings()
		{
            config = new XmlDocument();
            config.Load(GLOBAL_SETTINGS_FILE);
            blockedChars = new List<char>();

            string bchars = config.DocumentElement["xss"]["invalidchars"].Attributes["values"].Value;
            foreach (char c in bchars)
            {
                blockedChars.Add(c);
            }

            mimetypes = new MimeTypes(config.DocumentElement["mimetypes"]);
            apps = new HttpApplicationCollection(this);

            
            
        }

        public XmlNode Configuration
        {
            get
            {
                return config.DocumentElement;
            }
        }

        

        public string LastEmail
        {
            get
            {
                return config.DocumentElement["lastemail"].InnerText;
            }
            set
            {
                config.DocumentElement["lastemail"].InnerText = value;
            }
        }

        public bool BlockXSS
        {
            get
            {
                return bool.Parse(config.DocumentElement["xss"]["block"].InnerText);
            }
            set
            {
                config.DocumentElement["xss"]["block"].InnerText = value.ToString();
            }
        }

        public List<char> BlockedCharacters
        {
            get
            {
                return this.blockedChars;
            }
        }

        

        public int RequestHandlerInstances
        {
            get
            {
                return int.Parse(config.DocumentElement["requesthandlerinstances"].InnerText);
            }
        }


        public int Port
        {
            get
            {
                return int.Parse(config.DocumentElement["port"].InnerText);
            }
            set
            {
                config.DocumentElement["port"].InnerText = value.ToString();
            }
        }

       

        public HttpApplicationCollection HttpApplications
        {
            get
            {
                return apps;
            }
        }

        public MimeTypes ServerMimeTypes
		{
			get
			{
				return this.mimetypes;
			}
		}

        public void SaveSettings()
        {
            config.Save(GLOBAL_SETTINGS_FILE);
        }
    }

   


}
